package main.com.cyz.LinkList;

import main.com.cyz.LinkList.Bean.ListNode;

/**
 * @author fox
 * @version 1.0
 * @description
 * 力扣206
 * 给你单链表的头节点 head ，请你反转链表，并返回反转后的链表。
 * 示例 1：
 *
 *
 * 输入：head = [1,2,3,4,5]
 * 输出：[5,4,3,2,1]
 * 示例 2：
 *
 *
 * 输入：head = [1,2]
 * 输出：[2,1]
 * 示例 3：
 *
 * 输入：head = []
 * 输出：[]
 *
 *
 * 提示：
 *
 * 链表中节点的数目范围是 [0, 5000]
 * -5000 <= Node.val <= 5000
 * @date 2024/5/17 14:55
 */
public class LeetCode206 {

    //建立新节点，存放数据
    public static ListNode buildNewNode(ListNode head){
        ListNode result = new ListNode(-1,null);
        while(head!=null){
            ListNode node = new ListNode(head.val,result.next);
            result.next = node;
            head = head.next;
        }
        return result.next;
    }

    //移动节点至新链表，不构造新节点
    public static ListNode moveOldNode(ListNode head){
        List list1 = new List(head);
        List list2 = new List(null);
        while(true){
            ListNode remove = list1.remove();
            if (remove == null){
                break;
            }
            list2.add(remove);
        }
        return list2.head;
    }

    static class List{
        ListNode head;

        public List(ListNode head) {
            this.head = head;
        }

        public void add(ListNode node){
            node.next = head;
            head = node;
        }

        public ListNode remove(){
            if (head == null){
                return null;
            }
            ListNode node = head;
            head = head.next;
            return node;
        }
    }

    //递归
    public static ListNode recursion(ListNode head){
        if (head == null || head.next == null){
            return head;
        }
        ListNode nextNode = recursion(head.next);
        head.next.next = head;
        head.next = null;
        return nextNode;
    }

    //指针
    public static ListNode point(ListNode o1){
        if (o1 == null || o1.next == null){
            return o1;
        }
        ListNode o2 = o1.next;
        ListNode n1 = o1;
        while (o2 != null){
            o1.next = o2.next;
            o2.next = n1;
            n1 = o2;
            o2 = o1.next;
        }
        return n1;
    }
}
